import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { ItemData } from "../resource/info/ItemData";
import { GameProxy } from "../mvc/GameProxy";
import { CopyDrawRewardPanel } from "../view/copy/drawRewards/CopyDrawRewardPanel";
import { CopyDrawRewardComp } from "../view/copy/drawRewards/CopyDrawRewardComp";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { EProp } from "../../../Message/Public/EProp";
type int = number;
//class HappyBagController
    
    export  class HappyBagController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.PackUsePropHappyBag, this.onHappyBagUse);
            Dispatcher.addEventListener(EventName.PackBulkUsePropHappyBag, this.onHappyBagBulkUse);
            return;
        }

        /* internal  */onHappyBagUse(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            this.drawHappyBag(loc1);
            return;
        }

        /* internal  */onHappyBagBulkUse(arg1: DataEvent): void
        {
            var loc1=arg1.data.data as ItemData;
            var loc2=arg1.data.amount as int;
            if (loc2 != 1) 
            {
                if (loc2 > 1) 
                {
                    GameProxy.happyBag.openHappyBagMultiple(loc1.uid, loc2);
                }
            }
            else 
            {
                this.drawHappyBag(loc1);
            }
            return;
        }

        /* internal  */drawHappyBag(arg1: ItemData): void
        {
            if (!this._drawPanel) 
            {
                this._drawPanel = new CopyDrawRewardPanel(true);
                this._drawPanel.addEventListener(CopyDrawRewardComp.DrawComplete, this.onDrawComplete);
            }
            if (this._drawPanel.isDrawing()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60042));
            }
            else 
            {
                this._drawPanel.setDrawItem(arg1);
                this._drawPanel.show();
            }
            return;
        }

        /* internal  */onDrawComplete(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc1=arg1.data as Boolean;
            if (loc1) 
            {
                loc2 = this._drawPanel ? this._drawPanel.getDrawItem() : null;
                if (loc2) 
                {
                    loc2 = cache.pack.backPackCache.getItemByCode(loc2.itemCode);
                    if (loc2) 
                    {
                        this.drawHappyBag(loc2);
                        return;
                    }
                }
                if ((loc3 = cache.pack.backPackCache.getPropByType(EProp._EPropHappyBag)).length > 0) 
                {
                    loc4 = loc3[0] as ItemData;
                    this.drawHappyBag(loc4);
                }
            }
            return;
        }

        private /* var */_drawPanel: CopyDrawRewardPanel;
    }
